package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import adamsLibrary.JComboBoxAuto;
import adamsLibrary.JComboBoxAutoLinkedList;

import control.OrderControl;

public class SearchDeliveryPanel extends JPanel {
	private OrderControl oc;
	private JComboBoxAuto customerComboBox;
	private JTextField barcodeTextField;
	private JList list;
	private DefaultListModel listModel;
	private JLabel lblBarcode;
	private JLabel lblDate;
	private JLabel label;
	private JLabel lblBarcode_1;
	
	
	public SearchDeliveryPanel(OrderControl oc) {
		setBackground(Color.BLACK);
		this.oc=oc;

		listModel = new DefaultListModel();
		list = new JList(listModel);
		list.setBackground(Color.BLACK);
		list.setForeground(Color.GREEN);
		
		customerComboBox = new JComboBoxAuto(new GetCustomerList());
		JButton btnBarcodes = new JButton("Show info");
		btnBarcodes.addActionListener(new searchCustomerDeliveryAction());
		
		lblBarcode = new JLabel("Barcode");
		lblBarcode.setForeground(Color.GREEN);
		
		lblDate = new JLabel("Date");
		lblDate.setForeground(Color.GREEN);
		
		label = new JLabel("");
		label.setForeground(Color.GREEN);
		
		lblBarcode_1 = new JLabel("Customer");
		lblBarcode_1.setForeground(Color.GREEN);
		
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblBarcode_1)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(list, GroupLayout.PREFERRED_SIZE, 432, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblBarcode)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblDate, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(label)
									.addGap(157))
								.addComponent(customerComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnBarcodes)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(7)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(label)
							.addGap(81)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblBarcode)
								.addComponent(lblDate))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(list, GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblBarcode_1)
							.addComponent(customerComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(btnBarcodes)))
					.addContainerGap())
		);
		setLayout(groupLayout);
		
	}
	
	private class GetCustomerList implements JComboBoxAutoLinkedList{	
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getCustomers(string);
		}		
	}
	private class searchCustomerDeliveryAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String customer = customerComboBox.getEditor().getItem().toString();
			LinkedList<String> infoList = oc.getCustomerDeliveryInfo(customer);
			listModel.clear();
			for(String info:infoList){
				listModel.addElement(info);
			}
			
		}	
	}
	
	
	
}
